<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use PHPMailer\PHPMailer\PHPMailer;
use think\Cache;

class Register extends Controller
{
    public function index()
    {
        // 注册页面
        return view('index@Register/index',[
            'title' => '我是注册用户页面'
        ]);
    }

     public function yanzheng()
    {
        dump($_GET);exit;
    }

     public function cache()
    {
        // dump(session_save_path());die;
        if (!Cache::store('redis')->get('name')) {
            $data = date('Y-m-d H:i:s', time());
            Cache::store('redis')->set('name',$data,10);
        }
        $this->assign('time', Cache::store('redis')->get('name'));
        return $this->fetch('index/index');
    }

    public function insert(Request $request)
    {


         // var_dump($_POST);
//        $email = $_POST['email'];
//        $random =  $this->random(6,0);
//        $keydate = time();
//         //实例化
//        $mail=new PHPMailer();
//        try{
//            //邮件调试模式
//            $mail->SMTPDebug = 2;
//            //设置邮件使用SMTP
//            $mail->isSMTP();
//            // 设置邮件程序以使用SMTP
//            $mail->Host = 'smtp.163.com';
//            // 设置邮件内容的编码
//            $mail->CharSet='UTF-8';
//            // 启用SMTP验证
//            $mail->SMTPAuth = true;
//            // SMTP username
//            $mail->Username = '15021076332@163.com';
//            // SMTP password
//            $mail->Password = 'dihuazhixiu123';
//            // 启用TLS加密，`ssl`也被接受
//           $mail->SMTPSecure = 'ssl';
//            // 连接的TCP端口
//           $mail->Port = 465;
//            //设置发件人
//            $mail->setFrom('15021076332@163.com', 'whale');
//           //  添加收件人1
//            $mail->addAddress($email, 'sm');     // Add a recipient
//            $mail->addAddress('ellen@example.com');               // Name is optional
//            收件人回复的邮箱
            // $mail->addReplyTo('15021076332@163.com', 'whale');
//            抄送
//            $mail->addCC('cc@example.com');
//            $mail->addBCC('bcc@example.com');
            //附件
//            $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//            $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
            //Content
            // 将电子邮件格式设置为HTML
//            $mail->isHTML(true);
//            $mail->Subject = '邮件主题';
//            $mail->Body    = "注册成功，你在本站注册的邮箱需要验证！请点击<a href='http://localhost/index/checkid/?emailkey=".passport_encrypt($emailkey)."&email=".$emailID."'>http://localhost/index/checkid/?emailkey=".passport_encrypt($emailkey)."&email=".$emailID."</a>(或者复制到浏览器打开)，完成验证！";
//
////            $mail->AltBody = '这是非HTML邮件客户端的纯文本';
//            $mail->send();
//            echo 'Message has been sent';
//             // $mail->isSMTP();
//        }catch (Exception $e){
//            echo 'Mailer Error: ' . $mail->ErrorInfo;
//        }


        $res = $request->post();
//        var_dump($res);die;
        if($res['password'] == $res['repassword']){
            $res['password'] = md5($res['password']);
            $res['createtime'] = time();
            $res['status'] = 1;

            unset($res['source']);
            unset($res['redir']);
            unset($res['verify_phone']);
            unset($res['repassword']);

            $list = db('h_user')
                    ->insert($res);
            $str = db('h_user')->where('phone', $res['phone'])->find();

            Session::set($str['id']);
            Session::set($str['name']);
            Session::set($str['email']);
            Session::set($str['phone']);

             return $this->redirect('index/Login/index');
//            echo 'success';
        }


    }

}